package com.hongxun.design.adapters;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.FaceSendAndReceiveActivity;
import com.hongxun.design.bean.BitTypeTabItemBean;
import com.hongxun.design.bean.ProjectItemBean;
import com.hongxun.design.bean.service.ClientInfo;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.object_properties.MyMessage;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.imageutil.UriUtil;

import java.util.HashMap;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/4/27.
 */
public class ClientInfoGridAdapter extends BaseAdapter {
    List<ClientInfo> itemBeanList;
    Context context;
    LayoutInflater inflater;
    ProjectItemBean bean;

    public ClientInfoGridAdapter(List<ClientInfo> itemBeanList, Context context) {
        this.itemBeanList = itemBeanList;
        this.context = context;
        inflater = LayoutInflater.from(context);
        bean = FastJsonUtil.JsonToGetProject(((FaceSendAndReceiveActivity) context).projectcode);
    }

    @Override
    public int getCount() {
        return itemBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        if (convertView == null)
            convertView = inflater.inflate(R.layout.gridview_item_clientinfo, null);

        ClientInfo itemBean = (ClientInfo) getItem(i);
        ViewHolerHelper viewHolerHelper = new ViewHolerHelper(convertView);
        RelativeLayout item = viewHolerHelper.getView(R.id.userinfo_item);
        SimpleDraweeView icon = viewHolerHelper.getView(R.id.clientinfo_icon);
        TextView check = viewHolerHelper.getView(R.id.clientinfo_checked);
        TextView name = viewHolerHelper.getView(R.id.clientinfo_name);
        SetTypeface.set(check, IconSourse.checked);
        item.setOnClickListener(new MClickListener(i, itemBean) {
            @Override
            public void onClick(View view, int position, Object msg) {
                ClientInfo bean = (ClientInfo) msg;
                if (bean.ischecked()) {
                    bean.setIschecked(false);
                } else {
                    bean.setIschecked(true);
                }
                notifyDataSetChanged();
            }
        });
        icon.setImageURI(UriUtil.getUri(itemBean.getUserAvatar()));
        name.setText(itemBean.getUserName());
        if (itemBean.ischecked()) {
            check.setVisibility(View.VISIBLE);
            if (!CheckId(itemBean.getUserId())){
                check.setVisibility(View.GONE);
                itemBean.setIschecked(false);
                handler.sendMessage(MyMessage.getMessage(0,itemBean.getUserName()));
            }
        } else {
            check.setVisibility(View.GONE);
        }
        return convertView;
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ToastShow.showShort(context,msg.obj+"没有项目权限,无法传输！");
        }
    };
    private boolean CheckId(String userid) {
        if (userid.length()==0)return false;
        return !(userid.length() == 0 || (!bean.getAuditor().contains(userid) && !bean.getManager().contains(userid)
                && !bean.getExplorationer().contains(userid) && !bean.getDesigner().contains(userid)));
    }

    public class ViewHolerHelper {
        private HashMap<Integer, View> mViews;
        View convertView;

        public ViewHolerHelper(View convertView) {
            mViews = new HashMap<Integer, View>();
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item_clientinfo, null);
            }
            convertView.setTag(this);
            this.convertView = convertView;
        }

        public <T> T getView(Integer R_Id) {
            View view = null;
            if (!mViews.containsKey(R_Id)) {
                view = convertView.findViewById(R_Id);
                if (view != null) {
                    mViews.put(R_Id, view);
                }
            } else {
                view = mViews.get(R_Id);
            }
            return (T) view;
        }
    }

}
